/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package com.chonry.web2.services;

import com.chonry.web2.daos.MarketingBatchRecordDao;
import com.chonry.web2.daos.SmsFileDao;
import com.chonry.web2.models.SmsFile;
import com.chonry.web2.models.TblAppMarketingBatchRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.*;
import java.util.*;
import java.util.Date;

@Service
public class DemoService {

    @Resource
    private MarketingBatchRecordDao dao;

    @Autowired
    @Qualifier("shardingDataSource")
    private DataSource shardingDataSource;

    @Resource
    private SmsFileDao smsFileDao;

    public void ids() {
        Map<String, Object> params = new HashMap<>();
        params.put("batchId", 887);
        params.put("customerId", 10000);
        List<Map<String, Object>> list = dao.selectForPages(params);
        System.out.println(list.size());
        try {
            System.out.println(smsFileDao.selectByPrimaryKey(10));
        } catch (Exception e) {
            e.printStackTrace();
        }
        ;
        try {
            List<SmsFile> smsFile = smsFileDao.selects();
            System.out.println(smsFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void insertSharding() {
        Timestamp time = new Timestamp(new Date().getTime());
        try (
                Connection connection = shardingDataSource.getConnection();
                PreparedStatement ps = connection.prepareStatement(
                        "insert into tbl_app_marketing_batch_record(batchId,customerId,mobile,MobileOperator,content" +
                                ",smCount,validateFlag,smUuid,suggestChannelId,cTime,ltime)" +
                                "values(?,?,?,?,?,?,?,?,?,?,?)");
        ) {
            for (int j = 0; j <= 1; j++) {
                for (int i = 0; i < 11; i++) {
                    ps.setInt(1, i);
                    ps.setInt(2, j + 1);
                    ps.setString(3, "13999999999");
                    ps.setInt(4, 1);
                    ps.setString(5, "nihao,nihao");
                    ps.setInt(6, 1);
                    ps.setInt(7, 1);
                    ps.setString(8, UUID.randomUUID().toString().replace("-", ""));
                    ps.setInt(9, 1);
                    ps.setTimestamp(10, time);
                    ps.setTimestamp(11, time);
                    ps.addBatch();
                }
            }
            ps.executeBatch();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void insert() {
        SmsFile smsFile = new SmsFile();
        smsFile.setCustomerId(1);
        smsFile.setPath("/jjjjjjjjjjjjjjj.txt");
        smsFile.setCDate(new Date());
        smsFileDao.insert(smsFile);
        System.out.println("**********************");
        System.out.println(smsFile.getId());
    }

}
